<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(ROOT_PATH."class/task.class.php");
require_once(ROOT_PATH."class/team.class.php");

$team = new team;
//$dateformat = 'yyyy-mm-dd hh:ii';
$dateformat = 'yyyy-mm-dd';

if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$teamlist = $loader->loadList($team);	
} else {
	$team = new team;
	$team->id = $CURRENT_USER->teamid;
	$loader->get($team);
	$teamlist = array($team);
	$teamobj=$team;
}

if ($param['op']=='add' and $flow_ok) {
	$tsk = new task;
	$tsk->teamid=$param['teamid'];
	
	$df=split('[\ :/.-]',$dateformat);
	$startdate = split('[\ :/.-]',$param['startdate']);
	$enddate = split('[\ :/.-]',$param['enddate']);

	for ($i=0;$i<count($df);$i++) {
		if (strpos($df[$i],"yyyy")!==false) {
			$sa=$startdate[$i];
		} elseif (strpos($df[$i],"mm")!==false) {
			$sm=$startdate[$i];
		} elseif (strpos($df[$i],"dd")!==false) {
			$sd=$startdate[$i];
		} elseif (strpos($df[$i],"hh")!==false) {
			$sh=$startdate[$i];
		} elseif (strpos($df[$i],"ii")!==false) {
			$si=$startdate[$i];
		}
	}
	
	for ($i=0;$i<count($df);$i++) {
		if (strpos($df[$i],"yyyy")!==false) {
			$ea=$enddate[$i];
		} elseif (strpos($df[$i],"mm")!==false) {
			$em=$enddate[$i];
		} elseif (strpos($df[$i],"dd")!==false) {
			$ed=$enddate[$i];
		} elseif (strpos($df[$i],"hh")!==false) {
			$eh=$enddate[$i];
		} elseif (strpos($df[$i],"ii")!==false) {
			$ei=$enddate[$i];
		}
	}
	

	//$startdate = util::toUTC(mktime(0,0,0,$sm,$sd,$sa),$CURRENT_USER->timezone);
	//$enddate = util::toUTC(mktime(23,59,59,$em,$ed,$ea),$CURRENT_USER->timezone);
	$startdate = util::formatDate(mktime(0,0,0,$sm,$sd,$sa));
	$enddate = util::formatDate(mktime(23,59,59,$em,$ed,$ea));	
	$tsk->startdate=$startdate;
	$tsk->enddate=$enddate;
	$tsk->task=$param['task'];
	$loader->save($tsk);
} elseif ($param['op']=='edit' and $flow_ok) {
	$tsk = new task;
	$tsk->id=$param['id'];
	$ret = $loader->get($tsk);
	if ($ret) {
		$tsk->teamid=$param['teamid'];

		$df=split('[\ :/.-]',$dateformat);
		$startdate = split('[\ :/.-]',$param['startdate']);
		$enddate = split('[\ :/.-]',$param['enddate']);
	
		for ($i=0;$i<count($df);$i++) {
			if (strpos($df[$i],"yyyy")!==false) {
				$sa=$startdate[$i];
			} elseif (strpos($df[$i],"mm")!==false) {
				$sm=$startdate[$i];
			} elseif (strpos($df[$i],"dd")!==false) {
				$sd=$startdate[$i];
			} elseif (strpos($df[$i],"hh")!==false) {
				$sh=$startdate[$i];
			} elseif (strpos($df[$i],"ii")!==false) {
				$si=$startdate[$i];
			}
		}
		
		for ($i=0;$i<count($df);$i++) {
			if (strpos($df[$i],"yyyy")!==false) {
				$ea=$enddate[$i];
			} elseif (strpos($df[$i],"mm")!==false) {
				$em=$enddate[$i];
			} elseif (strpos($df[$i],"dd")!==false) {
				$ed=$enddate[$i];
			} elseif (strpos($df[$i],"hh")!==false) {
				$eh=$enddate[$i];
			} elseif (strpos($df[$i],"ii")!==false) {
				$ei=$enddate[$i];
			}
		}
		//$startdate = util::toUTC(mktime(0,0,0,$sm,$sd,$sa),$CURRENT_USER->timezone);
		//$enddate = util::toUTC(mktime(23,59,59,$em,$ed,$ea),$CURRENT_USER->timezone);
		$startdate = util::formatDate(mktime(0,0,0,$sm,$sd,$sa));
		$enddate = util::formatDate(mktime(23,59,59,$em,$ed,$ea));
		$tsk->startdate=$startdate;
		$tsk->enddate=$enddate;
		$tsk->task=$param['task'];	
		$loader->update($tsk);
		$message=$LANG['updated'];
	}
} elseif ($param['op']=='remove' and $flow_ok) {
	
	foreach ($param['itemid'] as $id) {
		$tsk = new task;
		$tsk->id=$id;
		$ret = $loader->delete($tsk);
	}
}

$tsk = new task;

if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$list = $loader->loadList($tsk);
} elseif ($CURRENT_USER->groupid == GROUP_COORD) {
	$tsk->teamid=$CURRENT_USER->teamid;
	$list = $loader->loadListBy($tsk,$tsk->listByTeam());
} else {
	$tsk->teamid=$CURRENT_USER->teamid;
	$list = $loader->loadListBy($tsk,$tsk->listActiveTask());
}

foreach ($list as $key => $obj) {
	foreach ($teamlist as $tkey => $tobj) {
		if ($obj->teamid == $tobj->id) {
			$obj->team=$tobj;	
		}	
	}
}

debug($list);

$tm->assign('dateformat',$dateformat);
$tm->assign('list',$list);
$tm->assign('teamlist',$teamlist);
$content = $tm->fetch('tasklist.tpl');

?>
